﻿using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

namespace Assets.Scripts.ProceduralCityGenerator
{
	// Token: 0x0200033C RID: 828
	public class CityBuilding : MonoBehaviour, IDestructible
	{
		// Token: 0x1700035E RID: 862
		// (get) Token: 0x06001565 RID: 5477 RVA: 0x0008C430 File Offset: 0x0008A630
		// (set) Token: 0x06001566 RID: 5478 RVA: 0x0008C438 File Offset: 0x0008A638
		public Vector3 Position { get; set; }

		// Token: 0x1700035F RID: 863
		// (get) Token: 0x06001567 RID: 5479 RVA: 0x0008C441 File Offset: 0x0008A641
		// (set) Token: 0x06001568 RID: 5480 RVA: 0x0008C449 File Offset: 0x0008A649
		public bool LowEnd { get; set; }

		// Token: 0x17000360 RID: 864
		// (get) Token: 0x06001569 RID: 5481 RVA: 0x0008C452 File Offset: 0x0008A652
		// (set) Token: 0x0600156A RID: 5482 RVA: 0x0008C45A File Offset: 0x0008A65A
		public CityDestructionManager DestructionManager { get; set; }

		// Token: 0x17000361 RID: 865
		// (get) Token: 0x0600156B RID: 5483 RVA: 0x0008C463 File Offset: 0x0008A663
		public bool broken
		{
			get
			{
				return this.mDestructionStatus != null && this.mDestructionStatus.IsBroken();
			}
		}

		// Token: 0x0600156C RID: 5484 RVA: 0x0008C480 File Offset: 0x0008A680
		private void Start()
		{
			if (base.transform.parent == null)
			{
				return;
			}
			this.thisCollider = base.GetComponentInChildren<Collider>();
			if (CityBuilding.debrisPrefab == null)
			{
				CityBuilding.debrisPrefab = Resources.Load<GameObject>("City/Debris");
			}
			CityBuilding.smokeEffectEnabled = GlobalPreferences.SmokeEnabled.value;
			if (CityBuilding.smokeParticles == null || CityBuilding.smokeData.Main.customSimulationSpace == null)
			{
				CityBuilding.smokeParticles = UnityEngine.Object.Instantiate<GameObject>(Resources.Load<GameObject>("Particles/SmokeParticles"));
				CityBuilding.smokeData = new ParticleData(CityBuilding.smokeParticles);
				CityBuilding.smokeData.Main.customSimulationSpace = base.transform.parent.parent;
			}
			if (CityBuilding.smokeParticleList == null)
			{
				CityBuilding.smokeParticleList = new List<ParticleData>();
			}
			this.mChunks = base.GetComponentInChildren<FracturedObject>();
			this.mDestructionStatus = new DestructionStatus(this.mChunks);
		}

		// Token: 0x0600156D RID: 5485 RVA: 0x0008C57C File Offset: 0x0008A77C
		private IEnumerator DebrisAnimation()
		{
			Vector3 originalPosition = base.transform.localPosition;
			GameObject debris = UnityEngine.Object.Instantiate<GameObject>(CityBuilding.debrisPrefab);
			debris.layer = ((!GlobalPreferences.KeepDebris.value) ? Layers.mapLayer : Layers.buildingLayer);
			debris.transform.SetParent(base.transform.parent, false);
			debris.transform.localPosition = originalPosition;
			debris.transform.localScale *= 0.5f;
			debris.transform.Rotate(Vector3.forward, UnityEngine.Random.Range(0f, 360f));
			Vector3 initialScale = debris.transform.localScale;
			float finalScale = 1f + UnityEngine.Random.value;
			float time = 0.1f;
			while (time < 6f)
			{
				debris.transform.localScale = initialScale * finalScale * time / 6f;
				time += Time.deltaTime;
				yield return null;
			}
			yield break;
		}

		// Token: 0x0600156E RID: 5486 RVA: 0x0008C598 File Offset: 0x0008A798
		private IEnumerator FallAnimation(float power)
		{
			this.falling = true;
			this.mDestructionStatus.AnimationCalled();
			this.thisCollider.enabled = false;
			Quaternion initialRotation = base.transform.localRotation;
			Quaternion targetRotation = Quaternion.Euler(UnityEngine.Random.Range(0f, this.maxRotation), base.transform.localEulerAngles.y, UnityEngine.Random.Range(0f, this.maxRotation));
			float time = 0.1f;
			this.PlayDestructionSound(power);
			while (time < 6f)
			{
				float fallSpeed = 0.5f * this.fallAcceleration * time * time;
				base.transform.localPosition -= Vector3.up * fallSpeed * Time.deltaTime;
				base.transform.localRotation = Quaternion.Slerp(base.transform.localRotation, targetRotation, 0.5f * Time.deltaTime);
				time += Time.deltaTime;
				yield return null;
			}
			UnityEngine.Object.Destroy(base.gameObject);
			yield break;
		}

		// Token: 0x0600156F RID: 5487 RVA: 0x0008C5BC File Offset: 0x0008A7BC
		public void IgnoreCollision(Collider externalCollider)
		{
			Collider componentInChildren = base.GetComponentInChildren<Collider>();
			Physics.IgnoreCollision(componentInChildren, externalCollider);
		}

		// Token: 0x06001570 RID: 5488 RVA: 0x0008C5D8 File Offset: 0x0008A7D8
		public void Collapse()
		{
			this.mDestructionStatus.AnimationCalled();
			try
			{
				this.mChunks.Explode(this.Position, 1f, 100000f, false, false, false, false);
				this.DestructionManager.UnregisterBuilding(this);
			}
			catch
			{
			}
			finally
			{
				this.falling = true;
				UnityEngine.Object.Destroy(base.gameObject, 20f);
			}
		}

		// Token: 0x06001571 RID: 5489 RVA: 0x0008C65C File Offset: 0x0008A85C
		private void PlayDestructionSound(float power)
		{
			float time = Time.time;
			if (time < CityBuilding.timeLastSound + CityBuilding.minimumDistanceBetweenSounds)
			{
				return;
			}
			CityBuilding.timeLastSound = time;
			if (power < 10f)
			{
				power = 10f;
			}
			else if (power > 400f)
			{
				power = 400f;
			}
			if (CityBuilding.smokeEffectEnabled)
			{
				this.SmokeEffect(power);
			}
			if (CityBuilding.audioSource == null || CityBuilding.audioSource[CityBuilding.sourceToUse] == null)
			{
				CityBuilding.audioSource = new AudioSource[CityBuilding.sourcesNumber];
				for (int i = 0; i < CityBuilding.sourcesNumber; i++)
				{
					CityBuilding.audioSource[i] = new GameObject("City Audio").AddComponent<AudioSource>();
					CityBuilding.audioSource[i].transform.SetParent(base.transform.parent);
					CityBuilding.audioSource[i].dopplerLevel = 0f;
					CityBuilding.audioSource[i].spatialBlend = 1f;
					CityBuilding.audioSource[i].minDistance = 50f;
					CityBuilding.audioSource[i].maxDistance = 5000f;
					CityBuilding.audioSource[i].outputAudioMixerGroup = SoundManager.mg_destruction;
				}
			}
			if (!CityBuilding.audioSource[CityBuilding.sourceToUse].isPlaying)
			{
				CityBuilding.audioSource[CityBuilding.sourceToUse].transform.localPosition = base.transform.localPosition;
				CityBuilding.audioSource[CityBuilding.sourceToUse].PlayOneShot(SoundManager.This.GetDestructionSound());
			}
			CityBuilding.sourceToUse++;
			if (CityBuilding.sourceToUse >= CityBuilding.sourcesNumber)
			{
				CityBuilding.sourceToUse = 0;
			}
		}

		// Token: 0x06001572 RID: 5490 RVA: 0x0008C7FD File Offset: 0x0008A9FD
		public void Destroy(Vector3 contactPoint, float scale, bool secondary, bool debri, bool cheap = false)
		{
			if (this.falling)
			{
				return;
			}
			this.falling = true;
			if (this.LowEnd || cheap)
			{
				this.LowEndDestruction(scale);
			}
			else
			{
				this.DynamicDestruction(secondary, debri);
			}
		}

		// Token: 0x06001573 RID: 5491 RVA: 0x0008C83C File Offset: 0x0008AA3C
		private void DynamicDestruction(bool secondary, bool debri)
		{
			if (debri)
			{
				this.mHits++;
				if (this.mHits < 20)
				{
					return;
				}
			}
			this.DestructionManager.RegisterBuilding(this);
			for (int i = 0; i < base.gameObject.transform.childCount; i++)
			{
				Transform child = base.gameObject.transform.GetChild(i);
				if (child.name == "U_Char")
				{
					child.gameObject.SetActive(false);
				}
				else if (child.name == "Chunks")
				{
					try
					{
						child.gameObject.SetActive(true);
						this.PrepareChunks(child, secondary);
					}
					catch
					{
						Debug.LogWarning("Failed to set chunks for structure " + base.gameObject.name + " vertex might not be a convex!");
					}
				}
			}
			base.StartCoroutine(this.DebrisAnimation());
		}

		// Token: 0x06001574 RID: 5492 RVA: 0x0008C940 File Offset: 0x0008AB40
		private void PrepareChunks(Transform chunksParrent, bool secondary)
		{
			int childCount = chunksParrent.childCount;
			for (int i = 0; i < childCount; i++)
			{
				Transform child = chunksParrent.GetChild(i);
				this.SetCrushing(child);
				CityBuilding.SetDestructable(child, secondary);
			}
		}

		// Token: 0x06001575 RID: 5493 RVA: 0x0008C97C File Offset: 0x0008AB7C
		private void SetCrushing(Component child)
		{
			child.gameObject.AddComponent<DebrisHandler>();
		}

		// Token: 0x06001576 RID: 5494 RVA: 0x0008C98C File Offset: 0x0008AB8C
		private static void SetDestructable(Component child, bool secondary)
		{
			if (!GlobalPreferences.SecondaryDestruction.value)
			{
				return;
			}
			Destructor destructor = child.gameObject.AddComponent<Destructor>();
			destructor.baseSize = 1000f;
			destructor.Secondary = secondary;
		}

		// Token: 0x06001577 RID: 5495 RVA: 0x0008C9C7 File Offset: 0x0008ABC7
		private void LowEndDestruction(float scale)
		{
			base.StartCoroutine(this.FallAnimation(scale));
			base.StartCoroutine(this.DebrisAnimation());
		}

		// Token: 0x06001578 RID: 5496 RVA: 0x0008C9E4 File Offset: 0x0008ABE4
		private void SmokeEffect(float power)
		{
			CityBuilding.smokeData.ParticleRoot.position = base.transform.position;
			CityBuilding.smokeData.Main.startSize = power * this.BuildingScale;
			CityBuilding.smokeData.Shape.radius = power * this.BuildingScale * 1.0072f;
			CityBuilding.smokeData.System.Play();
		}

		// Token: 0x0400182B RID: 6187
		private static GameObject debrisPrefab;

		// Token: 0x0400182C RID: 6188
		private static AudioSource[] audioSource;

		// Token: 0x0400182D RID: 6189
		private static int sourcesNumber = 5;

		// Token: 0x0400182E RID: 6190
		private static float minimumDistanceBetweenSounds = 1f;

		// Token: 0x0400182F RID: 6191
		private static float timeLastSound = 0.5f;

		// Token: 0x04001830 RID: 6192
		private static int sourceToUse;

		// Token: 0x04001831 RID: 6193
		private static GameObject smokeParticles;

		// Token: 0x04001832 RID: 6194
		private static List<ParticleData> smokeParticleList;

		// Token: 0x04001833 RID: 6195
		private static ParticleData smokeData;

		// Token: 0x04001834 RID: 6196
		public static bool smokeEffectEnabled;

		// Token: 0x04001838 RID: 6200
		public int xSize = 1;

		// Token: 0x04001839 RID: 6201
		public int zSize = 1;

		// Token: 0x0400183A RID: 6202
		public Vector3[] CellArea;

		// Token: 0x0400183B RID: 6203
		public float BuildingScale;

		// Token: 0x0400183C RID: 6204
		private Collider thisCollider;

		// Token: 0x0400183D RID: 6205
		private float maxRotation = 30f;

		// Token: 0x0400183E RID: 6206
		private int mHits;

		// Token: 0x0400183F RID: 6207
		private const int cDebriHitPoints = 20;

		// Token: 0x04001840 RID: 6208
		private float fallAcceleration = 5f;

		// Token: 0x04001841 RID: 6209
		private bool falling;

		// Token: 0x04001842 RID: 6210
		private FracturedObject mChunks;

		// Token: 0x04001843 RID: 6211
		private DestructionStatus mDestructionStatus;

		// Token: 0x04001844 RID: 6212
		private const float cDestroyDelay = 20f;

		// Token: 0x04001845 RID: 6213
		private const float cEndTimeAnimation = 6f;
	}
}
